<?php 
class setting_adminMenu extends MX_Controller{
	public function index(){
		$adminMenu= $this->load->module('adminMenu/index');
		$this->template='admin';
		if(isset($_POST)){
			foreach ($_POST as $key=>$value){
				if(!is_array($value))$data[$key]=trim($value);
				else $data[$key]=$value;
			}
		}
		if(isset($_POST['Add_AdminMenu'])){
			unset($data['Add_AdminMenu']);
			$this->model()->add_AdminMenu($data);
		}
		
		if(isset($_POST['Update_AdminMenu'])){
			unset($data['Update_AdminMenu']);
			$this->model()->update_AdminMenu($data);
		}
		
		
		$search='';
		if(isset($_POST['search_item'])){
			$search=$_POST['search_item'];
		}
		$tableItemBody=$this->model()->listItems($search);
		$option_Menu=self::dropdown_menu();
		$data=array(
					'update_footer'=>'test',
					'admin_title'=>'Control Admin',
					'option_menu'=>$option_Menu,
					'admin_menu'=>$adminMenu->generateAdminMenu(),
					'tableItemBody'=>$tableItemBody
				);
		$this->view(
				array('admin-header',
						'setting-adminMenu',
						'admin-footer'), 
				$data); // Display the page
	}
	public function dropdown_menu( $currentcat = 0,  $parent = 0, $level = 0, $category = '' ) {
	
		
		$categories = $this->model()->get_categories( $parent );
	
		if ( is_array($categories) and !empty($categories) ) {
			foreach ( $categories as $cat ) {
				$pad = str_repeat( '&#8211; ', $level );
				$s= ( $currentcat == $cat->id )?" selected='selected'":'';
				$catOption=  "<option value='{$cat->id}' $s >$pad{$cat->name}</option>\n\t";
				$category .=self::dropdown_menu( $currentcat, $cat->id, $level +1, $catOption );
			}
		}
		return $category;
	}

	public function down($id){
		$copyId=array();
		$ids=$this->model()->listIdShameParent($id);
		foreach($ids as $index=>$orderId){
			if($orderId==$id){
				$down=$orderId;
				continue;
			}
			$copyId[]=$orderId;
			if(isset($down)){
				$copyId[]=$down;
				unset($down);
			}
		}
		foreach($copyId as $order=>$id)
			$OrderArray[$id]=array('order'=>$order);
		$this->model()->updateOrder($OrderArray);
		$this->load->helper('url');
		redirect('setting/adminMenu');
	}
	
	public function up($id){
		$copyId=array();
		$ids=$this->model()->listIdShameParent($id);
		foreach($ids as $index=>$orderId){
			if($orderId==$id){
				$up=array_pop($copyId);
			}
			$copyId[]=$orderId;
			if(isset($up)){
				$copyId[]=$up;
				unset($up);
			}
		}
		foreach($copyId as $order=>$id)
			$OrderArray[$id]=array('order'=>$order);
		$this->model()->updateOrder($OrderArray);
	
		$this->load->helper('url');
		redirect('setting/adminMenu');
	}

	public function delete($id){
		$check_subCategory=$this->model()->get_categories($id);
	
		if(count($check_subCategory)==0){
			$this->model()->delete_categories($id);
		}else{
			foreach ($check_subCategory as $subCategory)
				$subCatName[]=$subCategory->name;
			if(!isset($_SESSION)) session_start();
			$_SESSION['notification'][]="Ce Menu n'est pas vide / supprimer sous-menu (".implode(', ',$subCatName).")";
		}
		$this->load->helper('url');
		redirect('setting/adminMenu');
	}
}